package com.ljw;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JPanel;


class Board4 extends JPanel{
	private	final	int	B_WIDTH				=	350;
	private	final	int	B_HEIGHT				=	350;
	private	final	int	INITIAL_X				=	-40;
	private	final	int	INITIAL_Y				=	-40;
	private	final	int	INITIAL_DELAY		=	100;
	private	final	int	PERIOD_INTERVAL	=	25;
	
	private Image hahaha;
	private Timer timer;
	private int x, y;
	
	public Board4(){
		initBoard();
	}

	private void initBoard() {
		setBackground(Color.black);
		setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
		setDoubleBuffered(true);
		
		loadImage();
		
		x = INITIAL_X;
		y = INITIAL_Y;
		
		timer = new Timer();
		timer.scheduleAtFixedRate(new ScheduleTask(), INITIAL_DELAY, PERIOD_INTERVAL);
	}

	private void loadImage() {
		ImageIcon ii = new ImageIcon("./resource/img/hahaha.jpg");
		hahaha = ii.getImage();
	}
	
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		drawImage(g);
	}
	
	private void drawImage(Graphics g) {
		g.drawImage(hahaha, x, y, this);
		Toolkit.getDefaultToolkit().sync();
	}

	private class ScheduleTask extends TimerTask{

		@Override
		public void run() {
			x++;
			y++;
			
			if(y > B_HEIGHT){
				x = INITIAL_X;
				y = INITIAL_Y;
			}
			repaint();
		}
	}
}